package server

import (
	"./bean"
	"./utils"
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)

func LoginTask(w http.ResponseWriter, req *http.Request) {
	fmt.Println("loginTask is running...")

	//获取客户端通过GET/POST方式传递的参数
	req.ParseForm()
	body123, _ := ioutil.ReadAll(req.Body)
	fmt.Println(req.Method)
	fmt.Println("1" + string(body123))
	param_userName, found1 := req.Form["userName"]
	param_password, found2 := req.Form["password"]
	if !(found1 && found2) {
		fmt.Fprint(w, "请勿非法访问")
		return
	}

	result := new(bean.LoginResult)
	userName := param_userName[0]
	password := param_password[0]

	s := "userName:" + userName + ",password:" + password
	fmt.Println(s)

	if userName == "sauce" && password == "123456" {
		result.Code = 200
		result.Message = "登录成功"
		tims := time.Now().Unix()
		result.Time = tims
		result.Token = utils.GetGuid()
		//dataBean := &DataBean{}
		//dataBean.authToken = md5.New()
		//result.Data = dataBean
	} else {
		result.Code = 101
		result.Message = "用户名或密码不正确"
	}

	//向客户端返回JSON数据
	bytes, _ := json.Marshal(result)
	w.Header().Add("Access-Control-Allow-Origin", "*")
	w.Header().Add("Access-Control-Allow-Headers", "X-Requested-With")
	w.Header().Add("Access-Control-Allow-Credentials", "true")
	w.Header().Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS,DELETE,PUT")
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprint(w, string(bytes))
	req.Body.Close()
}
func Get48Live(w http.ResponseWriter, req *http.Request) {
	gHeader(w)
	//向客户端返回JSON数据

	req.ParseForm()
	param_time, found3 := req.Form["time"]
	time := param_time[0]
	if !(found3) {
		fmt.Fprint(w, "请勿非法访问")
		return
	}
	result := new(bean.BaseResult)
	liveRequest, _ := json.Marshal(bean.LiveRequest{LastTime: time, Limit: 20, GiftUpdTime: 1498211389003})
	fmt.Println(string(liveRequest))
	req, err := http.NewRequest("POST", "https://plive.48.cn/livesystem/api/live/v1/memberLivePage", bytes.NewReader(liveRequest))
	//rrr, err := http.Post("", "application/json", strings.NewReader(string(liveRequest)))
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("version", "1.1.0")
	req.Header.Set("os", "android")
	client := &http.Client{}
	rrr, err := client.Do(req)

	zxc, _ := ioutil.ReadAll(rrr.Body)
	resultt := new(bean.LiveResult)
	errw := json.Unmarshal(zxc, &resultt)
	if errw != nil {
		fmt.Println("ERROR", errw)
	}
	json.Marshal(resultt)

	if resultt.Content.ReviewList != nil {
		for index := 0; index < len(resultt.Content.ReviewList); index++ {
			ssss := resultt.Content.ReviewList[index]
			stringss := strings.Split(ssss.PicPath, ",")
			lll := make([]string, len(stringss))
			for jjj := 0; jjj < len(stringss); jjj++ {
				picPath := stringss[jjj]
				picPath = "https://source3.48.cn/resize_120X120" + picPath
				lll[jjj] = picPath
			}
			resultt.Content.ReviewList[index].PicPath = lll[0]
			resultt.Content.ReviewList[index].PicPaths = lll
		}
	}
	if resultt.Content.LiveList != nil {
		for index := 0; index < len(resultt.Content.LiveList); index++ {
			ssss := resultt.Content.LiveList[index]
			stringss := strings.Split(ssss.PicPath, ",")
			lll := make([]string, len(stringss))
			for jjj := 0; jjj < len(stringss); jjj++ {
				picPath := stringss[jjj]
				picPath = "https://source3.48.cn/resize_120X120" + picPath
				lll[jjj] = picPath
			}
			resultt.Content.LiveList[index].PicPath = lll[0]
			resultt.Content.LiveList[index].PicPaths = lll
		}
	}

	fmt.Println(string(zxc))
	if err != nil {
		fmt.Println(err.Error())
	}
	bytes, _ := json.Marshal(resultt)
	result.Code = 200
	//result.Message = string(body)
	fmt.Fprint(w, string(bytes))
}
func GetLive(w http.ResponseWriter, req *http.Request) {
	gHeader(w)
	req.ParseForm()
	param_time, found3 := req.Form["liveId"]
	liveId := param_time[0]
	if !(found3) {
		fmt.Fprint(w, "请勿非法访问")
		return
	}
	result := new(bean.BaseResult)
	liveRequest, _ := json.Marshal(bean.OneLiveRequest{LiveType: 1, UserId: 477193, LiveId: liveId})
	fmt.Println(string(liveRequest))
	req, err := http.NewRequest("POST", "https://plive.48.cn/livesystem/api/live/v1/getLiveOne", bytes.NewReader(liveRequest))
	//rrr, err := http.Post("", "application/json", strings.NewReader(string(liveRequest)))
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("version", "1.1.0")
	req.Header.Set("os", "android")
	client := &http.Client{}
	rrr, err := client.Do(req)

	zxc, _ := ioutil.ReadAll(rrr.Body)
	resultt := new(bean.OneLiveResult)
	errw := json.Unmarshal(zxc, &resultt)
	if errw != nil {
		fmt.Println("ERROR", errw)
	}
	json.Marshal(resultt)

	stringss := strings.Split(resultt.Content.PicPath, ",")
	lll := make([]string, len(stringss))
	for jjj := 0; jjj < len(stringss); jjj++ {
		picPath := stringss[jjj]
		picPath = "https://source3.48.cn/resize_120X120" + picPath
		lll[jjj] = picPath
	}
	resultt.Content.PicPath = lll[0]
	//resultt.Content.ReviewList[index].PicPaths = lll


	fmt.Println(string(zxc))
	if err != nil {
		fmt.Println(err.Error())
	}
	bytes, _ := json.Marshal(resultt)
	result.Code = 200
	//result.Message = string(body)
	fmt.Fprint(w, string(bytes))
}
func gHeader(w http.ResponseWriter) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
	w.Header().Set("Access-Control-Allow-Headers", "Action, Module")
	w.Header().Set("Content-Type", "application/json")
}
